package DT8;

import javagprssrv.Common.DateTime;
import java.text.DecimalFormat;
import java.sql.ResultSet;
import javagprssrv.DataBase.DBSQLExecute;
import java.util.Date;
import java.text.SimpleDateFormat;

public class DT8_Util
{
    static char[] auchCRCHi;
    static char[] auchCRCLo;
    
    public java.lang.String DT8_CRC(final java.lang.String Data) {
        java.lang.String res = "";
        int crcHi = 255;
        int crcLo = 255;
        int index = 0;
        for (int i = 0; i < Data.length(); ++i) {
            final char c = Data.charAt(i);
            index = (crcHi ^ c);
            crcHi = (crcLo ^ DT8.DT8_Util.auchCRCHi[index]);
            crcLo = DT8.DT8_Util.auchCRCLo[index];
        }
        res = "" + (char)crcLo + (char)crcHi;
        return res;
    }
    
    public java.lang.String DT8_GetSJM() {
        java.lang.String res = "";
        final java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final java.util.Date D_70 = new java.util.Date();
        D_70.setTime(0L);
        final java.util.Date D_now = new java.util.Date();
        final long sec = (D_now.getTime() - D_70.getTime()) / 1000L;
        final int sjm0 = (int)(sec / 16777216L);
        final int sjm2 = (int)(sec / 65536L) % 256;
        final int sjm3 = (int)(sec / 256L) % 256;
        final int sjm4 = (int)(sec % 256L);
        res = "" + (char)sjm4 + (char)sjm3 + (char)sjm2 + (char)sjm0;
        return res;
    }
    
    public java.lang.String DT8_GetSJMfromDate(final java.lang.String strDate) {
        java.lang.String res = "";
        final java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date D_70 = null;
        try {
            D_70 = sdf.parse("1970-01-01 00:00:00");
        }
        catch (java.lang.Exception ex) {}
        java.util.Date D_now = null;
        try {
            D_now = sdf.parse(strDate);
        }
        catch (java.lang.Exception ex2) {}
        final long sec = (D_now.getTime() - D_70.getTime()) / 1000L;
        final int sjm0 = (int)(sec / 16777216L);
        final int sjm2 = (int)(sec / 65536L) % 256;
        final int sjm3 = (int)(sec / 256L) % 256;
        final int sjm4 = (int)(sec % 256L);
        res = "" + (char)sjm4 + (char)sjm3 + (char)sjm2 + (char)sjm0;
        return res;
    }
    
    public java.lang.String DT8_GetDatefromSJM(final java.lang.String SJM) {
        java.lang.String res = "";
        final java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date D_70 = null;
        try {
            D_70 = sdf.parse("1970-01-01 00:00:00");
        }
        catch (java.lang.Exception ex) {}
        java.util.Date D_71 = null;
        try {
            D_71 = sdf.parse("1970-01-01 00:00:00");
        }
        catch (java.lang.Exception ex2) {}
        D_71.setTime(1000L);
        long sp = 0L;
        sp = sp + SJM.charAt(0) + 'Ā' * SJM.charAt(1) + 65536 * SJM.charAt(2) + 16777216 * SJM.charAt(3);
        sp -= 28800L;
        final java.util.Date D_now = null;
        D_70.setTime(sp * 1000L);
        res = sdf.format(D_70);
        return res;
    }
    
    public java.lang.String DT8_FillCommand(final java.lang.String Command) {
        java.lang.String res = "";
        int len = Command.length();
        len -= 4;
        len %= 16;
        len = 16 - len;
        res = "\u0080";
        for (int i = 0; i < len - 1; ++i) {
            res = res + "" + '\0';
        }
        return res;
    }
    
    public java.lang.String DT8_FillCommand2(java.lang.String Command) {
        java.lang.String res = "";
        int len = Command.length();
        len -= 4;
        len %= 16;
        len = 16 - len;
        res = "\u0080";
        for (int i = 0; i < len - 1; ++i) {
            res = res + "" + '\0';
        }
        Command = Command + "" + res;
        return res;
    }
    //数据库结构不符
    public java.lang.String DT8Data2DT3Data(final java.lang.String DT8Data) {
        java.lang.String res = "";
        final java.lang.String _WYM_17 = DT8Data.substring(8, 25);
        final java.lang.String _WYM_18 = _WYM_17.substring(6);
        final java.lang.String sql = "select YB.*,GS.报表小数位,yb2.行政区域码,yb2.通讯方式 from T_YB YB left outer join T_GS GS on GS.公司名称=YB.公司名称 left outer join T_YB2 yb2 on yb2.仪表号=yb.仪表号 where 唯一码='" + _WYM_18 + "'";
        final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
        final java.sql.ResultSet rs = dbse.SQL_Query(0, sql);
        java.lang.String sYB_BL = "";
        java.lang.String DYDJ = "";
        java.lang.String DYDW = "";
        try {
            rs.next();
            double YB_BL = rs.getFloat("倍率");
            sYB_BL = rs.getString("倍率");
            DYDJ = rs.getString("电压等级").trim();
            DYDW = rs.getString("单位").trim();
            if (DYDW.toUpperCase().equals("V")) {
                YB_BL = 1.0;
            }
            if (DYDW.toUpperCase().equals("KV")) {
                YB_BL = 0.001;
            }
        }
        catch (java.lang.Exception ex) {}
        java.lang.String _SJM_DataTime = DT8Data.substring(30, 34);
        final java.lang.String _TJS_ZYXS = DT8Data.substring(43, 45);
        final java.lang.String _TJS_HGS = DT8Data.substring(45, 47);
        final java.lang.String _TJS_CSXS = DT8Data.substring(47, 49);
        final java.lang.String _TJS_CXXS = DT8Data.substring(49, 51);
        final java.lang.String _DYZ_PJZ = DT8Data.substring(51, 55);
        final java.lang.String _DYZ_Min = DT8Data.substring(55, 59);
        java.lang.String _SJM_MinTime = DT8Data.substring(59, 63);
        final java.lang.String _DYZ_Max = DT8Data.substring(63, 67);
        java.lang.String _SJM_MaxTime = DT8Data.substring(67, 71);
        _SJM_DataTime = new DT8.DT8_Util().DT8_GetDatefromSJM(_SJM_DataTime);
        _SJM_MinTime = new DT8.DT8_Util().DT8_GetDatefromSJM(_SJM_MinTime);
        _SJM_MaxTime = new DT8.DT8_Util().DT8_GetDatefromSJM(_SJM_MaxTime);
        final java.lang.String s2_Year = _SJM_DataTime.substring(2, 4);
        final java.lang.String s2_Month = _SJM_DataTime.substring(5, 7);
        final java.lang.String s2_Date = _SJM_DataTime.substring(8, 10);
        java.lang.String zyxs = java.lang.String.valueOf(_TJS_ZYXS.charAt(0) + _TJS_ZYXS.charAt(1) * 'Ā');
        for (int i = 5; i > zyxs.length(); zyxs = "0" + zyxs, --i) {}
        final java.lang.String hgs = java.lang.String.valueOf(_TJS_HGS.charAt(0) + _TJS_HGS.charAt(1) * 'Ā');
        java.lang.String csxs = java.lang.String.valueOf(_TJS_CSXS.charAt(0) + _TJS_CSXS.charAt(1) * 'Ā');
        for (int j = 5; j > csxs.length(); csxs = "0" + csxs, --j) {}
        java.lang.String cxxs = java.lang.String.valueOf(_TJS_CXXS.charAt(0) + _TJS_CXXS.charAt(1) * 'Ā');
        for (int k = 5; k > cxxs.length(); cxxs = "0" + cxxs, --k) {}
        final double pjz = this.StringData2Double(_DYZ_PJZ);
        final double zxz = this.StringData2Double(_DYZ_Min);
        final java.lang.String zxzs = _SJM_MinTime.substring(5, 7) + _SJM_MinTime.substring(8, 10) + _SJM_MinTime.substring(11, 13) + _SJM_MinTime.substring(14, 16);
        final double zdz = this.StringData2Double(_DYZ_Max);
        final java.lang.String zdzs = _SJM_MaxTime.substring(5, 7) + _SJM_MaxTime.substring(8, 10) + _SJM_MaxTime.substring(11, 13) + _SJM_MaxTime.substring(14, 16);
        res = "A000";
        if (DT8Data.charAt(26) == '\u0005') {
            res += '\u0004';
        }
        else {
            res += '\u0003';
        }
        res = res + "0000" + s2_Year + s2_Month + s2_Date;
        res += this.FillDataString(zyxs, 5, "0");
        res += this.FillDataString(csxs, 5, "0");
        res += this.FillDataString(cxxs, 5, "0");
        res = res + this.DoubleData2String(zdz, 4) + zdzs;
        res = res + this.DoubleData2String(zxz, 4) + zxzs;
        res += this.DoubleData2String(pjz, 4);
        for (int l = 0; l < 96; ++l) {
            res += "0";
        }
        res = res + "Z00" + "YY13812345678" + "0000";
        return res;
    }
    
    private double StringData2Double(final java.lang.String data) {
        int i0 = data.charAt(0);
        i0 = java.lang.Math.round((float)(i0 / 16)) * 10 + i0 % 16;
        int i2 = data.charAt(1);
        i2 = java.lang.Math.round((float)(i2 / 16)) * 10 + i2 % 16;
        int i3 = data.charAt(2);
        i3 = java.lang.Math.round((float)(i3 / 16)) * 10 + i3 % 16;
        int i4 = data.charAt(3);
        i4 = java.lang.Math.round((float)(i4 / 16)) * 10 + i4 % 16;
        final double res = i0 * 0.01 + i2 * 1 + i3 * 100 + i4 * 10000;
        return res;
    }
    
    private java.lang.String DoubleData2String(final double data, final int length) {
        java.lang.String res = "";
        final java.text.DecimalFormat df = new java.text.DecimalFormat("0.0");
        java.lang.String s = df.format(data * 10000.0);
        if (java.lang.Integer.valueOf(s.substring(0, 1)) >= 7) {
            s = "0" + s;
        }
        res = s.substring(0, length);
        return res;
    }
    
    private java.lang.String FillDataString(final java.lang.String _Data, final int FillLength, final java.lang.String FillString) {
        java.lang.String res = "";
        final int len = _Data.length();
        if (len < FillLength) {
            for (int i = 0; i < FillLength - len; ++i) {
                res = FillString + res;
            }
            res += _Data;
        }
        else if (len > FillLength) {
            res = _Data.substring(0, FillLength);
        }
        else {
            res = _Data;
        }
        return res;
    }
    
    public java.lang.String MakeSetDateCommoand(final java.lang.String DT3_SetDateCommand) {
        java.lang.String res = "";
        java.lang.String cjsj = "";
        final java.lang.String hgl = "000";
        final java.lang.String csxl = "000";
        final java.lang.String cxxl = "000";
        java.lang.String zyxs = "";
        java.lang.String hgs = "";
        java.lang.String csxs = "";
        java.lang.String cxxs = "";
        java.lang.String pjz = "";
        java.lang.String zxz = "";
        java.lang.String zxzs = "";
        java.lang.String zdz = "";
        java.lang.String zdzs = "";
        cjsj = DT3_SetDateCommand.substring(0, 4);
        zyxs = DT3_SetDateCommand.substring(4, 9);
        csxs = DT3_SetDateCommand.substring(9, 14);
        cxxs = DT3_SetDateCommand.substring(14, 19);
        zdz = DT3_SetDateCommand.substring(19, 27);
        zdzs = DT3_SetDateCommand.substring(27, 35);
        zxz = DT3_SetDateCommand.substring(35, 43);
        zxzs = DT3_SetDateCommand.substring(43, 51);
        pjz = DT3_SetDateCommand.substring(51, 59);
        final java.lang.String Year = new javagprssrv.Common.DateTime().getDateTime("YYYYMMDDHHNNSS").substring(0, 4);
        cjsj = "" + this.DT8_GetSJMfromDate(Year + "-" + cjsj.substring(0, 2) + "-" + cjsj.substring(2, 4) + " 00:00:00");
        zdzs = "" + this.DT8_GetSJMfromDate(Year + "-" + zdzs.substring(0, 2) + "-" + zdzs.substring(2, 4) + " " + zdzs.substring(4, 6) + ":" + zdzs.substring(6, 8) + ":00");
        zxzs = "" + this.DT8_GetSJMfromDate(Year + "-" + zxzs.substring(0, 2) + "-" + zxzs.substring(2, 4) + " " + zxzs.substring(4, 6) + ":" + zxzs.substring(6, 8) + ":00");
        final int izyxs = java.lang.Integer.valueOf(zyxs);
        final int icsxs = java.lang.Integer.valueOf(csxs);
        final int icxxs = java.lang.Integer.valueOf(cxxs);
        final int ihgs = izyxs - icsxs - icxxs;
        zyxs = "" + (char)(izyxs % 256) + (char)(izyxs / 256);
        hgs = "" + (char)(ihgs % 256) + (char)(ihgs / 256);
        csxs = "" + (char)(icsxs % 256) + (char)(icsxs / 256);
        cxxs = "" + (char)(icxxs % 256) + (char)(icxxs / 256);
        zdz = this.DY2BCD(zdz);
        zxz = this.DY2BCD(zxz);
        pjz = this.DY2BCD(pjz);
        res = "" + cjsj + hgl + csxl + cxxl + zyxs + hgs + csxs + cxxs + pjz + zxz + zxzs + zdz + zdzs;
        return res;
    }
    
    public java.lang.String MakeSetMonthCommoand(java.lang.String DT3_SetMonthCommand) {
        java.lang.String res = "";
        DT3_SetMonthCommand = DT3_SetMonthCommand.substring(0, 2) + "01" + DT3_SetMonthCommand.substring(2);
        res = this.MakeSetDateCommoand(DT3_SetMonthCommand);
        return res;
    }
    
    java.lang.String DY2BCD(final java.lang.String strDY) {
        java.lang.String res = "";
        final int i0 = java.lang.Integer.valueOf(strDY.substring(0, 1)) * 16 + java.lang.Integer.valueOf(strDY.substring(1, 2));
        final int i2 = java.lang.Integer.valueOf(strDY.substring(2, 3)) * 16 + java.lang.Integer.valueOf(strDY.substring(3, 4));
        final int i3 = java.lang.Integer.valueOf(strDY.substring(4, 5)) * 16 + java.lang.Integer.valueOf(strDY.substring(5, 6));
        final int i4 = java.lang.Integer.valueOf(strDY.substring(6, 7)) * 16 + java.lang.Integer.valueOf(strDY.substring(7, 8));
        res = "" + (char)i4 + (char)i3 + (char)i2 + (char)i0;
        return res;
    }
    
    static {
        DT8.DT8_Util.auchCRCHi = new char[] { '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@', '\u0001', 'À', '\u0080', 'A', '\u0001', 'À', '\u0080', 'A', '\0', 'Á', '\u0081', '@' };
        DT8.DT8_Util.auchCRCLo = new char[] { '\0', 'À', 'Á', '\u0001', 'Ã', '\u0003', '\u0002', 'Â', 'Æ', '\u0006', '\u0007', 'Ç', '\u0005', 'Å', 'Ä', '\u0004', 'Ì', '\f', '\r', 'Í', '\u000f', 'Ï', 'Î', '\u000e', '\n', 'Ê', 'Ë', '\u000b', 'É', '\t', '\b', 'È', 'Ø', '\u0018', '\u0019', 'Ù', '\u001b', 'Û', 'Ú', '\u001a', '\u001e', 'Þ', 'ß', '\u001f', 'Ý', '\u001d', '\u001c', 'Ü', '\u0014', 'Ô', 'Õ', '\u0015', '×', '\u0017', '\u0016', 'Ö', 'Ò', '\u0012', '\u0013', 'Ó', '\u0011', 'Ñ', 'Ð', '\u0010', 'ð', '0', '1', 'ñ', '3', 'ó', 'ò', '2', '6', 'ö', '÷', '7', 'õ', '5', '4', 'ô', '<', 'ü', 'ý', '=', 'ÿ', '?', '>', 'þ', 'ú', ':', ';', 'û', '9', 'ù', 'ø', '8', '(', 'è', 'é', ')', 'ë', '+', '*', 'ê', 'î', '.', '/', 'ï', '-', 'í', 'ì', ',', 'ä', '$', '%', 'å', '\'', 'ç', 'æ', '&', '\"', 'â', 'ã', '#', 'á', '!', ' ', 'à', ' ', '`', 'a', '¡', 'c', '£', '¢', 'b', 'f', '¦', '§', 'g', '¥', 'e', 'd', '¤', 'l', '¬', '\u00ad', 'm', '¯', 'o', 'n', '®', 'ª', 'j', 'k', '«', 'i', '©', '¨', 'h', 'x', '¸', '¹', 'y', '»', '{', 'z', 'º', '¾', '~', '\u007f', '¿', '}', '½', '¼', '|', '´', 't', 'u', 'µ', 'w', '·', '¶', 'v', 'r', '²', '³', 's', '±', 'q', 'p', '°', 'P', '\u0090', '\u0091', 'Q', '\u0093', 'S', 'R', '\u0092', '\u0096', 'V', 'W', '\u0097', 'U', '\u0095', '\u0094', 'T', '\u009c', '\\', ']', '\u009d', '_', '\u009f', '\u009e', '^', 'Z', '\u009a', '\u009b', '[', '\u0099', 'Y', 'X', '\u0098', '\u0088', 'H', 'I', '\u0089', 'K', '\u008b', '\u008a', 'J', 'N', '\u008e', '\u008f', 'O', '\u008d', 'M', 'L', '\u008c', 'D', '\u0084', '\u0085', 'E', '\u0087', 'G', 'F', '\u0086', '\u0082', 'B', 'C', '\u0083', 'A', '\u0081', '\u0080', '@' };
    }
}
